package com.hue.nio.day02.one;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/*
    SocketChannel实现客户端
 */
public class Test02 {
    public static void main(String[] args) throws Exception {
        //创建客户端,绑定ip地址和端口号
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 7777));
        socketChannel.configureBlocking(false);
        //向服务端发送数据
        while (true) {
            Scanner scanner = new Scanner(System.in);
            String str = scanner.nextLine();
            ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
            buffer.put(str.getBytes());
            buffer.flip();
            socketChannel.write(buffer);
        }
    }
}
